<% allow_return_item_changes = !@return_authorization.customer_returned_items? %>

<div data-hook="admin_return_authorization_form_fields">
<div class="table-responsive">
  <table class="table table-condensed table-bordered return-items-table">
    <thead>
      <tr data-hook="rma_header">
        <th>
          <% if allow_return_item_changes %>
            <%= check_box_tag 'select-all' %>
          <% end %>
        </th>
        <th><%= Spree.t(:product) %></th>
        <th><%= Spree.t(:status) %></th>
        <th><%= Spree.t(:charged) %></th>
        <th><%= Spree.t(:purchased_quantity) %></th>
        <th><%= Spree.t(:return_quantity) %></th>
        <th><%= Spree.t(:pre_tax_refund_amount) %></th>
        <th><%= Spree.t(:reimbursement_type) %></th>
        <th><%= Spree.t(:exchange_for) %></th>
      </tr>
    </thead>
    <tbody>
      <%= f.fields_for :return_items, @form_return_items do |item_fields| %>
        <% return_item = item_fields.object %>
        <% inventory_unit = return_item.inventory_unit %>
        <% editable = inventory_unit.shipped? && allow_return_item_changes && return_item.reimbursement.nil? %>
        <tr>
          <td class="inventory-unit-checkbox">
            <% if editable %>
              <%= item_fields.hidden_field :inventory_unit_id %>
              <%= item_fields.check_box :_destroy, {checked: return_item.persisted?, class: 'add-item', "data-price" => return_item.pre_tax_amount}, '0', '1' %>
            <% end %>
          </td>
          <td>
            <div class="variant-name">
              <%= link_to return_item.inventory_unit.variant.name, spree.edit_admin_product_path(return_item.inventory_unit.variant.product) %>
            </div>
            <div class="variant-options"><%= inventory_unit.variant.options_text %></div>
          </td>
          <td><%= inventory_unit.state.humanize %></td>
          <td class="charged-amount" data-charged-amount="<%= return_item.pre_tax_amount %>">
            <%= return_item.display_pre_tax_amount %>
          </td>
          <td class="purchased-quantity">
            <%= inventory_unit.quantity %>
          </td>
          <td>
            <% if editable %>
              <%= item_fields.number_field :return_quantity, { class: 'refund-quantity-input form-control', min: 0, max: return_item.return_quantity } %>
            <% else %>
              <%= return_item.return_quantity %>
            <% end %>
          </td>
          <td>
            <% if editable %>
              <%= item_fields.text_field :pre_tax_amount, { class: 'refund-amount-input form-control' } %>
            <% else %>
              <%= return_item.display_pre_tax_amount %>
            <% end %>
          </td>
          <td>
            <% if editable %>
              <%= item_fields.select :preferred_reimbursement_type_id, @reimbursement_types.collect{|r|[r.name.humanize, r.id]}, {include_blank: true}, {class: 'select2-clear'} %>
            <% end %>
          </td>
          <td>
            <% if editable %>
              <% if return_item.exchange_processed? %>
                <%= return_item.exchange_variant.exchange_name %>
              <% else %>
                <%= item_fields.collection_select :exchange_variant_id, return_item.eligible_exchange_variants, :id, :exchange_name, { include_blank: true }, { class: "select2-clear return-item-exchange-selection" } %>
              <% end %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>
  <%= f.field_container :amount, class: ['alert alert-info'] do %>
    <%= Spree.t(:total_pre_tax_refund) %>: <span id="total_pre_tax_refund">0.00</span>
  <% end %>

  <%= f.field_container :stock_location, class: ['form-group'] do %>
    <%= f.label :stock_location, Spree.t(:stock_location) %> <span class="required">*</span>
    <%= f.select :stock_location_id, Spree::StockLocation.order_default.active.to_a.collect{|l|[l.name, l.id]}, {include_blank: true}, {class: 'select2-clear', "data-placeholder" => Spree.t(:select_a_stock_location)} %>
    <%= f.error_message_on :stock_location %>
  <% end %>

  <%= f.field_container :reason, class: ['form-group'] do %>
    <%= f.label :reason, Spree.t(:reason) %> <span class="required">*</span>
    <%= f.select :return_authorization_reason_id, @reasons.collect{|r|[r.name, r.id]}, {include_blank: true}, {class: 'select2-clear', "data-placeholder" => Spree.t(:select_a_return_authorization_reason)} %>
    <%= f.error_message_on :reason %>
  <% end %>

  <%= f.field_container :memo, class: ['form-group'] do %>
    <%= f.label :memo, Spree.t(:memo) %>
    <%= f.text_area :memo, class: 'form-control' %>
    <%= f.error_message_on :memo %>
  <% end %>
</div>

<% if Spree::Config[:expedited_exchanges] %>
  <div class="expedited-exchanges-warning"><%= Spree.t(:expedited_exchanges_warning, days_window: Spree::Config[:expedited_exchanges_days_window]) %></div>
<% end %>
